package com.pengheng.controller;

import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @remark
 * @Author pengheng
 * @date 2021/4/23 14:37
 */
@RestController
@RequestMapping("/jdk")
public class JDKLockController {

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/hello")
    public String hello() {
        Lock lock = new ReentrantLock();

        System.out.println("等待获取锁" + Thread.currentThread().getName());
        try {
            lock.lock();
            System.out.println("加锁成功，执行业务" + Thread.currentThread().getName());
            Thread.sleep(15000);
        } catch (Exception e) {
        } finally {
            lock.unlock();
            System.out.println("释放锁。。。" + Thread.currentThread().getName());
        }
        return "hello world";
    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    @GetMapping("/write")
    public String writeValue() {
        Lock writeLock = readWriteLock.writeLock();

        String s = "";
        try {
            writeLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(3000);
            redisTemplate.opsForValue().set("writeValue", s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            writeLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    public String readValue() {
        Lock readLock = readWriteLock.readLock();
        readLock.lock();
        String s = null;
        try {
            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readLock.unlock();
        }
        return s;
    }

    Semaphore semaphore = new Semaphore(5);
    @GetMapping("/park")
    public String park() throws InterruptedException {
        boolean b = semaphore.tryAcquire(10, TimeUnit.SECONDS); //带时间的获取，超过时间没获取到就为false
        return "ok->"+b;
    }

    @GetMapping("/go")
    public String go() throws InterruptedException {
        semaphore.release();//释放信号量
        return "ok";
    }

    CountDownLatch countDownLatch = new CountDownLatch(5);
    @GetMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        countDownLatch.await();//等待闭锁都完成
        return  "放假了";
    }

    @GetMapping("/gogogo/{id}")
    public String gogogo(@PathVariable("id") Long id) throws InterruptedException {
        countDownLatch.countDown();

        return id+"班的人走了";
    }
}
